using System;
namespace ServiceStack
{
	public static class RequestAttributesExtensions
	{
		public static bool IsLocalhost(this RequestAttributes attrs)
		{
			return (RequestAttributes.Localhost & attrs) == RequestAttributes.Localhost;
		}
		public static bool IsLocalSubnet(this RequestAttributes attrs)
		{
			return (RequestAttributes.LocalSubnet & attrs) == RequestAttributes.LocalSubnet;
		}
		public static bool IsExternal(this RequestAttributes attrs)
		{
			return (RequestAttributes.External & attrs) == RequestAttributes.External;
		}
		public static Format ToFormat(this string format)
		{
			Format result;
			try
			{
				result = (Format)Enum.Parse(typeof(Format), format.ToUpper().Replace("X-", ""), true);
			}
			catch (Exception)
			{
				result = Format.Other;
			}
			return result;
		}
		public static string FromFormat(this Format format)
		{
			string text = format.ToString().ToLower();
			if (format == Format.ProtoBuf || format == Format.MsgPack)
			{
				return "x-" + text;
			}
			return text;
		}
		public static Format ToFormat(this Feature feature)
		{
			if (feature <= Feature.Soap11)
			{
				if (feature <= Feature.Xml)
				{
					if (feature == Feature.Json)
					{
						return Format.Json;
					}
					if (feature == Feature.Xml)
					{
						return Format.Xml;
					}
				}
				else
				{
					if (feature == Feature.Jsv)
					{
						return Format.Jsv;
					}
					if (feature == Feature.Soap11)
					{
						return Format.Soap11;
					}
				}
			}
			else
			{
				if (feature <= Feature.Csv)
				{
					if (feature == Feature.Soap12)
					{
						return Format.Soap12;
					}
					if (feature == Feature.Csv)
					{
						return Format.Csv;
					}
				}
				else
				{
					if (feature == Feature.Html)
					{
						return Format.Html;
					}
					if (feature == Feature.ProtoBuf)
					{
						return Format.ProtoBuf;
					}
					if (feature == Feature.MsgPack)
					{
						return Format.MsgPack;
					}
				}
			}
			return Format.Other;
		}
		public static Feature ToFeature(this Format format)
		{
			if (format <= Format.Json)
			{
				if (format <= Format.Soap12)
				{
					if (format == Format.Soap11)
					{
						return Feature.Soap11;
					}
					if (format == Format.Soap12)
					{
						return Feature.Soap12;
					}
				}
				else
				{
					if (format == Format.Xml)
					{
						return Feature.Xml;
					}
					if (format == Format.Json)
					{
						return Feature.Json;
					}
				}
			}
			else
			{
				if (format <= Format.ProtoBuf)
				{
					if (format == Format.Jsv)
					{
						return Feature.Jsv;
					}
					if (format == Format.ProtoBuf)
					{
						return Feature.ProtoBuf;
					}
				}
				else
				{
					if (format == Format.Csv)
					{
						return Feature.Csv;
					}
					if (format == Format.Html)
					{
						return Feature.Html;
					}
					if (format == Format.MsgPack)
					{
						return Feature.MsgPack;
					}
				}
			}
			return Feature.CustomFormat;
		}
		public static Feature ToSoapFeature(this RequestAttributes attributes)
		{
			if ((RequestAttributes.Soap11 & attributes) == RequestAttributes.Soap11)
			{
				return Feature.Soap11;
			}
			if ((RequestAttributes.Soap12 & attributes) == RequestAttributes.Soap12)
			{
				return Feature.Soap12;
			}
			return Feature.None;
		}
	}
}
